<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Authority
require_once("controlHeader.php");					//--> system control header
include_once("Attributes.php");						//--> info class

$objOperate		= new Operation($objSession->getLanguage());		//--> Operation
$objAttributes 	= new Attributes(DB_TAG_SYSTEM, $uid);	//--> Info Class

$error_message 	= '';
$strName 		= '';
$strParentid	= '';
$strIsLeaf		= 'no';
$sorts			= -1;

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);

if(isset($_POST['Submit'])){
	$strParentid 	= $_POST['parentid'];
	$strName 		= $_POST['name'];
	$strIsLeaf 		= $_POST['isLeaf'];
	$sorts 			= $_POST['sorts'];
	$summary 		= $_POST['summary'];
	
	if($strParentid == "")
		$error_message = 'The UP to level class should\'t be empty.';
	else if(empty($strName))
		$error_message = 'The class name should\'t be empty.';
	else{
		$isReturn = $objAttributes->add($arrOperate, $strName, $strParentid, $sorts, $strIsLeaf, $summary);
		if($isReturn)
			$error_message = 'add successfully.';
		else 
			$error_message = 'add failure.';
	}
}

$arrParentid 	= array('0' => "root");
$arrIsLeaf 		= array('yes' => 'Yes', 'no' => 'No');

$arrTmpStack = array();
$arrLevelStack = array();
$i = 0;
array_push($arrTmpStack,'0');

while(sizeof($arrTmpStack) > 0){
	$strtmp = array_pop($arrTmpStack);
	$i = array_pop($arrLevelStack);
	if($strtmp > 0){
		$arrback = $objAttributes->getFromID($arrOperate, $LANGEUAGE_ID, $strtmp);
		if(is_array($arrback)){
			$tmp = '';
			for($j = 0; $j < $i; $j++){
				$tmp.='&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$arrParentid[$arrback['id']] = $tmp.$arrback['name'];
		}
	}

	$mixBack = $objAttributes->listFromCustom($arrOperate, " WHERE parentid='$strtmp'");
	if(is_array($mixBack)){
		$i++;
		foreach($mixBack as $value){
			array_push($arrTmpStack,$value['id']);
			array_push($arrLevelStack,$i);
		}
	}
}

/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrIsLeaf', $arrIsLeaf);
$smarty->assign('isLeafS', $strIsLeaf);

$smarty->assign('sorts', $sorts);
$smarty->assign('arrParentid', $arrParentid);
$smarty->assign('parentidS', $strParentid);
$smarty->assign('name', $strName);
$smarty->assign('summary', $summary);

$smarty->display('listAttributes_Add.htm');
?>
